'''
Created on Apr 26, 2011

@author: GSE
'''
import json
import urllib
import pymysql



class Webcrawler():
    def __init__(self):
        self.urlcapLists = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/"
        self.urlcapMovieSearch = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=d9d2ez5audh2df8fgwaxtpy5&q="
        self.urlCapMovieLists = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/"
    
    def extractUrlJson(self,url):
        #common JSON extraction
        content = urllib.urlopen(url).read()
        content = content.replace("\n","")
        target = json.loads(content)
        return target
        
    
    def searchMovie(self,movie,pagelimit):
        #Utility from Webcrawl0
        movie = movie.replace(" ","+")
        urlbottom = "&page_limit=" + str(pagelimit)
        url = self.urlcapMovieSearch + movie + urlbottom
        response = self.extractUrlJson(url)
        return response
    
    def getDvdReleases(self,pagelimit):
        urlbt = "new_releases.json?apikey=d9d2ez5audh2df8fgwaxtpy5&page_limit="+str(pagelimit)
        url = self.urlcapLists + urlbt
        response = self.extractUrlJson(url)
        return response
    
    def getMovieByLink(self,link):
        url = link + "?apikey=d9d2ez5audh2df8fgwaxtpy5"
        response = self.extractUrlJson(url)
        return response
    
    def getTheaterMovies(self,limit):
        url = self.urlCapMovieLists + "/in_theaters.json?apikey=d9d2ez5audh2df8fgwaxtpy5" + "&limit=" + str(limit)
        response = self.extractUrlJson(url)
        movies = []
        for i in response["movies"]:
            movies.append(i)
        return movies
    
    def openDBConn(self,ip,portdb,usr,pwd,database):
        self.conn = pymysql.connect(host=ip, port=portdb,user=usr,passwd=pwd,db=database)
        
    def closeDbConn(self):
        self.conn.close()
    
    def cleanTheaters(self,conn):
        cur = conn.cursor()
        cur.execute("delete from cr_theatermovies;")
        conn.commit()
        cur.close()
    
    def pushTheatersDatabase(self,conn,name,year,genre,pic):
        cur = conn.cursor()
        cur.execute("INSERT INTO cr_theatermovies(movname,movgenre,movDate,urlposter) VALUES('"+name+"','"+genre+"','"+year+"','"+pic+"');")
        print "Inserted into Database"
        conn.commit()
        cur.close()
        
    #def fetchTVDB(self):
        
        
class Content ():
    def __init__(self,title,ascore,syn,year,genre,ctype,actor,pic):
        self.title = title
        self.audienceScore = ascore
        self.synopsis = syn
        self.year = year
        self.genre = genre
        self.contentType = ctype
        self.actor1 = actor
        self.picture = pic

    def toStr(self):
        return self.title + "  "+ str(self.audienceScore)+"  "+ self.synopsis + "  " + str(self.year) + "  " + self.genre + " " + self.actor1 + " " + self.picture
    
    def pushToDatabase(self,conn):
        cur = conn.cursor()
        cur.callproc("insertContent" , (self.title,self.contentType,self.audienceScore,self.synopsis,self.genre,self.year,"From Rotten Tomatoes","Movies by crawler",1,self.picture))
        print "Inserted into Database"
        conn.commit()
        cur.close()
    # 

        
